package com.xhu.rjsx2.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.xhu.rjsx2.enums.AppHttpCodeEnum;
import com.xhu.rjsx2.pojo.Member;
import com.xhu.rjsx2.pojo.ResponseResult;
import com.xhu.rjsx2.pojo.User;
import com.xhu.rjsx2.pojo.dto.MemberDTO;
import com.xhu.rjsx2.pojo.dto.UserDTO;
import com.xhu.rjsx2.pojo.vo.LayuiPageResult;
import com.xhu.rjsx2.pojo.vo.PageVO;
import com.xhu.rjsx2.pojo.vo.UserVO;
import com.xhu.rjsx2.service.UploadService;
import com.xhu.rjsx2.service.UserService;
import com.xhu.rjsx2.util.BeanCopyUtils;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/user")
@Api(tags = "用户表（User）管理接口API", description = "User Controller")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private UploadService uploadService;

    @PostMapping("/upload")
    @ApiOperation(value = "上传头像接口")
    public ResponseResult uploadImg(@RequestParam("img") MultipartFile img){
        return uploadService.uploadImg(img);
    }

    @GetMapping("/listAllUser")
    @ApiOperation(value = "查询所有数据接口")
    public ResponseResult listAllUser(){
        List<User> userList = userService.list();
        List<UserVO> userVOList = BeanCopyUtils.copyBeanList(userList, UserVO.class);
        return ResponseResult.okResult(userVOList);
    }
    @GetMapping("/listAllUserPage")
    @ApiOperation(value = "列表分页查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页数"),
            @ApiImplicitParam(name = "limit", value = "每页条数"),
            @ApiImplicitParam(name = "name", value = "用户姓名"),
            @ApiImplicitParam(name = "phone", value = "电话号码"),
            @ApiImplicitParam(name = "age", value = "年龄")
    })
    public LayuiPageResult listAllMemberPage(Integer page, Integer limit,String name,String phone,Integer age){

        PageVO pageVO = userService.listAllUserPage(page, limit,name,phone,age);
        return new LayuiPageResult(LayuiPageResult.CODE_SUCCESS,LayuiPageResult.MESSAGE_SUCCESS,pageVO.getRows(), pageVO.getTotal());
    }

    @GetMapping("/{id}")
    public ResponseResult getUserById(@PathVariable("id") Integer id){
        return ResponseResult.okResult(userService.getById(id));
    }

    @PostMapping("/updateUserById")
    @ApiOperation(value = "通过主键更新用户信息")
    public ResponseResult updateUserById(@ApiParam(value = "用户传输对象",required = true) @RequestBody UserDTO userDTO){
        User user = BeanCopyUtils.copyBeanSingle(userDTO, User.class);
        boolean updateSuccess = userService.updateById(user);
        return updateSuccess ? ResponseResult.okResult(user) : ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }
    @PostMapping("/deleteUserById")
    @ApiOperation(value = "通过主键删除会员信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id")
    })
    public ResponseResult deleteMemberById(Integer id){
        boolean removeSuccess = userService.removeById(id);
        return removeSuccess ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }
    @PostMapping("/insertUser")
    @ApiOperation(value = "新增会员接口")
    public ResponseResult insertMember(@ApiParam(value = "会员对象",required = true) @RequestBody UserDTO userDTO){
        /*
        用户名
adsad
登录时间
2023-05-12 22:38:31
电话号码
fsaf
年龄
11
姓名
12
角色
         */

        // 禁止用户传入id，由系统生成
//        userDTO.setId(null);
        User user = BeanCopyUtils.copyBeanSingle(userDTO, User.class);
        String now = DateUtil.now();
        user.setLastLoginTime(DateUtil.parse(now));
        boolean saveSuccess = userService.save(user);
        return saveSuccess ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }

}
